import { createRouter, createWebHistory } from 'vue-router'
import { useTtsaStore } from '@/store/ttsa'
import { equ } from '@env'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'main',
      component: () => import(`@/views/${equ}/index.vue`),
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/',
    },
  ],
})

router.beforeEach((to, from, next) => {
  const { query } = to,
    { content_id, enter_without_node, content_node_id } = query
  if (content_id || content_node_id || enter_without_node) {
    const ttsa = useTtsaStore()
    ttsa.startParam = { content_id, node_id: content_node_id, enter_without_node }
    delete query.content_id
    delete query.content_node_id
    delete query.enter_without_node
    next({ name: to.name, query, replace: true })
  } else {
    next()
  }
})

export default router
